import {Component} from '@angular/core';
import {IonicPage, NavController, InfiniteScroll, PopoverController} from 'ionic-angular';
import {PagingConfig, PagingProvider} from "@providers/paging";
import {DialogProvider} from "@providers/dialog";
import {StorageProvider} from "@providers/storage";
import {Util} from "@providers/util";

@IonicPage()
@Component({
  selector: 'page-rcharge_list',
  templateUrl: 'rcharge_list.html',
})
export class RchargeListPage {

  datas: any[] = [];
  userInfo: any;
  datasConfig: PagingConfig = new PagingConfig();
  time: any = '7';
  timeStarts: string = Util.dateToString(new Date(new Date().setDate(new Date().getDate() - 7)));
  timeEnds: string = Util.dateToString(new Date());
  datasCache: any[] = [];//缓存datas
  max: string = Util.dateToString(new Date());

  constructor(private dialog: DialogProvider, private popoverCtrl: PopoverController,
              public navCtrl: NavController, private storageService: StorageProvider,
              private pagingService: PagingProvider) {
    this.userInfo = this.storageService.read("userInfo");
  }

  push(page: string, data?: any) {
    this.navCtrl.push(page, data);
  }

  ionViewDidLoad() {
    this.dialog.showLoading();
    this.getData(data => {
      this.datas = data;
      this.datasCache = data;
      if (data.length > 0) {
        for (let i = 0; i < this.datas.length; i++) {
          this.datas[i]['show'] = false;
        }
      }
    }, this.datasConfig);
  }

  showMaterial(item?) {
    item.show = !item.show;
  }


  /**
   * 获取数据
   * @param pagingConfig 分页
   * @param fn 方法
   */
  private getData(fn: (value) => void, pagingConfig) {
    this.pagingService.get({
      interfaceId: "csm.css.pay.getPaySorderMx_AllSJ",
      // data: {"WORKER": this.userInfo["PARTYID"],"FWLX":"2", 'time': this.time}
      data: {"TOSTATUS": ["customer"], "WORKER": this.userInfo["EXTID"], 'time': this.time}
      //data: {"TOSTATUS": ["customer"], "WORKER": this.userInfo["EXTID"]}
    }, pagingConfig).subscribe(data => {
      this.dialog.hideLoading();
      fn(data);
    }, error => {
      this.dialog.hideLoading();
    });
  }

  /**
   * 下拉刷新
   * @param infiniteScroll
   */
  getMoreDatas(infiniteScroll: InfiniteScroll) {
    this.getData((value) => {
      if (value.length > 0) {
        for (let i = 0; i < value.length; i++) {
          value[i]['show'] = false;
        }
      }
      this.datas.push.apply(this.datas, value);
      infiniteScroll.complete();
    }, this.datasConfig);
  }

  //打开条件弹窗
  presentPopover(myEvent) {
    let popover = this.popoverCtrl.create('SelectPopoverPage', {'time': this.time}, {cssClass: 'order_list_page'});
    popover.onDidDismiss(data => {
      if (data != null) {
        data != '0' ? this.time = data : this.time = null;
        this.datasConfig = new PagingConfig();
        this.getData(data => {
          this.datas = data;
          if (data.length > 0) {
            for (let i = 0; i < this.datas.length; i++) {
              this.datas[i]['show'] = false;
            }
          }
        }, this.datasConfig);
      }

    });
    popover.present({
      ev: myEvent
    });
  }

  //该方法暂时使用，有时间的时候改为接口
  timeQuery() {
    this.pagingService.get({
      interfaceId: "csm.css.pay.getPaySorderMx_AllSJ",
      data: {"TOSTATUS": ["customer"], "WORKER": this.userInfo["EXTID"] , 'STARTTIME': this.timeStarts , 'ENDTIME': this.timeEnds}
    }, new PagingConfig()).subscribe(data => {
      this.dialog.hideLoading();
      this.datas = data;
    }, error => {
      this.dialog.hideLoading();
    });

  }

}
